//
//  SYZXFDFuDaoDetailsView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/8/14.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYZXFDFuDaoDetailsView.h"
#import "SYFDDetailsTableViewCell.h"
#import "XueShengXiangQingVC.h"
#import "SYGroupChatDetailModel.h"
#import "SYKSXuanZeXueShengView.h"
#import "RYChatViewController.h"

@interface SYZXFDFuDaoDetailsView ()<UITableViewDelegate,UITableViewDataSource>
{
    RYChatViewController *chatVC;
    
}
@property (strong, nonatomic) SYBackTopView *topView;
@property (weak, nonatomic) IBOutlet UILabel *fuDaoName;//辅导名称
@property (weak, nonatomic) IBOutlet UILabel *duiYingKaoShiLab;//对应考试
@property (weak, nonatomic) IBOutlet UILabel *fudaoNierongLab;//辅导内容
@property (weak, nonatomic) IBOutlet UILabel *classNameLab;//班级名称
@property (weak, nonatomic) IBOutlet UILabel *fudaoTypeLab;//辅导类型
@property (weak, nonatomic) IBOutlet UILabel *fudaoLiYouLab;//辅导缘由
@property (weak, nonatomic) IBOutlet UILabel *kaishiTimeLab;//开始时间
@property (strong, nonatomic) NSArray *studentArr;//学生
@property (weak, nonatomic) IBOutlet UITableView *curTableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;

@property (strong, nonatomic) XueShengXiangQingVC *xueShengDetailVC;

@property (strong, nonatomic) SYGroupChatDetailModel *banjiDetails;

@property (nonatomic, strong) SYKSXuanZeXueShengView *xueshengView;


@end


@implementation SYZXFDFuDaoDetailsView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.curTableView.delegate = self;
    self.curTableView.dataSource = self;
    self.curTableView.tableFooterView = [UIView new];
//    self.curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.curTableView.hidden = YES;
    self.headerView.hidden = YES;
    self.curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.curTableView registerCellWithNIbNames:@[@"SYFDDetailsTableViewCell"]];
    [self addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(20);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(45);
    }];
}

- (void)requestDataClassId:(NSString *)classId {
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI fd_requestGroupChatClassDetailsClassId:classId success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        self.curTableView.hidden = NO;
        self.headerView.hidden = NO;
        [self setDataArrUpdateUIKitBanJiDetails:listArr.firstObject];
        
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.domain];
    }];
    
}

- (void)setDataArrUpdateUIKitBanJiDetails:(SYGroupChatDetailModel *)details {
    self.banjiDetails = details;
    self.studentArr = details.dataStudent;
    [self.curTableView reloadData];
    self.fuDaoName.text = SYStringWithPickFormat(@"辅导名称:", details.title);
    self.duiYingKaoShiLab.text = SYStringWithPickFormat(@"对应考试:", details.templateName);
    
    self.fudaoNierongLab.text = SYStringWithPickFormat(@"辅导内容:", details.content);
    self.classNameLab.text = SYStringWithPickFormat(@"班级名称:", details.className);
    self.fudaoTypeLab.text = SYStringWithPickFormat(@"辅导类型:", details.tutorTypeName);
    self.fudaoLiYouLab.text = details.typeName;
    self.kaishiTimeLab.text = SYStringWithPickFormat(@"开始时间:", details.beginDate);
}

#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.studentArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYFDDetailsTableViewCell *detailsCell = [tableView dequeueReusableCellWithIdentifier:@"SYFDDetailsTableViewCell" forIndexPath:indexPath];
    SYChatDataStudent *details = self.studentArr[indexPath.row];
    detailsCell.selectionStyle = UITableViewCellSelectionStyleNone;
    detailsCell.nameLab.text = details.name;
    detailsCell.statuLable.text = details.masterStatus;
    detailsCell.phoneLable.text = details.mobile;
    __weak typeof(self) weakSelf = self;
    NSMutableArray *nameArr = [NSMutableArray array];
    for (SYDataStudentParents *parents in details.listImInfo) {
        SYExamInfoStudentsModel *model = [[SYExamInfoStudentsModel alloc]init];
        model.studentName = parents.account;
        model.studentId = parents.imid;
        model.isSelect = NO;
        [nameArr addObject:model];
    }
    detailsCell.creatOneChatClick = ^{
        
        weakSelf.xueshengView = [[NSBundle mainBundle] loadNibNamed:@"SYKSXuanZeXueShengView" owner:nil options:nil].firstObject;
        weakSelf.xueshengView.isFrom = @"chat";
        [weakSelf.xueshengView showKaoShiBanJi:weakSelf.xueshengView dataArr:nameArr];
        
        weakSelf.xueshengView.block = ^(id obj, id objTwo) {
                NSString *imidStr = (NSString *)obj;
                NSString *nameStr = (NSString *)objTwo;
//                [weakSelf requestContAPI];
            [weakSelf setChatWithOnePeopleWithUserinfo:nameStr WithImid:imidStr];
        };
    };
    return detailsCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SYChatDataStudent *details = self.studentArr[indexPath.row];
    _xueShengDetailVC = [[XueShengXiangQingVC alloc]initWothStudentID:details.xsid];
    _xueShengDetailVC.view.backgroundColor = [UIColor whiteColor];
    //_xueShengDetailVC.view.frame = CGRectMake(self.mainw, 0, self.mainw, APP_HEIGHT);
    [self addSubview:_xueShengDetailVC.view];
    [_xueShengDetailVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@20);
        make.bottom.equalTo(@0);
    }];
    
}

#pragma mark -
- (SYBackTopView *)topView {
    if (!_topView) {
        _topView = [[SYBackTopView alloc] initWithTitleName:@"本次辅导信息" baseView:self];
    }
    return _topView;
}
-(void)setChatWithOnePeopleWithUserinfo:(NSString *)userName WithImid:(NSString *)imid{
    
    if (chatVC != nil) {
        [chatVC.view removeFromSuperview];
        chatVC = nil;
    }
    chatVC = [[RYChatViewController alloc] init];
    chatVC.IsFrom = @"detail";
    chatVC.conversationType = ConversationType_PRIVATE;
    chatVC.targetId = imid;
    chatVC.UserName = userName;
    chatVC.view.frame = CGRectMake(0, 0, APP_WIDTH-300, APP_HEIGHT - 20);
    chatVC.view.userInteractionEnabled = YES;
    
    chatVC.detailVCBack = ^{
        [self->chatVC.view removeFromSuperview];
    };
    [self addSubview:chatVC.view];
//    [self addChildViewController:chatVC];
}

@end
